<?php

namespace App\Module;

use App\depository\PermissionsDep;
use App\depository\RoleHasPermissionsDep;
use App\depository\RolesDep;
use App\depository\ApiLogDep;
use App\Enum\ApiLogEnum;
use App\Enum\CommonEnum;
use App\Enum\PermissionsEnum;
use App\Enum\RolesEnum;
use App\Enum\TeamsEnum;
use App\Service\DictService;
use App\depository\UserDep;

class ApiLogModule extends BaseModule
{
    public function init($request)
    {

        $param = $request->input();

        $dictService = new DictService();
        $dictService->setUsers()->setTeamStatus();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();

        if (empty($param['name'])) {
            return self::response([], '请输入工会名称', 100);
        }

        $dep = new ApiLogDep();
//        $res = $dep->firstByName($param['name']);
//        if($res){
//            return self::response([],'工会名称已存在，请重新输入',200);
//        }

        $data = [
            'name' => $param['name'],
            'status' => $param['status'],
            'leader_id' => !empty($param['leader_id']) ? $param['leader_id'] : -1,
            'expire_at' => $param['expire_at']
        ];

        $dep->add($data);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new ApiLogDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        if (empty($param['name'])) {
            return self::response([], '请输入工会名称', 100);
        }

        $dep = new ApiLogDep();
        $data = [
            'name' => $param['name'],
            'status' => $param['status'],
            'leader_id' => !empty($param['leader_id']) ? $param['leader_id'] : -1,
            'expire_at' => $param['expire_at']
        ];

        $dep->upd($param['id'], $data);

        return self::response();

    }

    public function selfList($request)
    {
        $param = $request->input();
        $user = $request->user();

        $dep = new ApiLogDep();
        $userDep = new UserDep();
        $param['user_id'] = $user->id;

        $resList = $dep->list($param);

        $data['list'] = $resList->map(function ($item) use ($userDep, $user) {
            return [
                'id' => $item->id,
                'ip' => $item->ip,
                'title' => ApiLogEnum::$statusArr[$item->path] ?? CommonEnum::DEFAULT_NULL,
                'user_id' => $item->user_id,
                'path' => $item->path,
                'method' => $item->method,
                'input' => "无权查看",
                'created_at' => $item->created_at->toDateTimeString()
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }

    public function list($request)
    {
        $param = $request->input();
        $user = $request->user();

        $dep = new ApiLogDep();
        $userDep = new UserDep();
        $param['user_id'] = $user->id;

        $resList = $dep->list($param);

        $data['list'] = $resList->map(function ($item) use ($userDep,$user) {
            $resUser = $userDep->firstById($item->user_id);
            return [
                'id' => $item->id,
                'ip' => $item->ip,
                'title' => ApiLogEnum::$statusArr[$item->path] ?? CommonEnum::DEFAULT_NULL,
                'user_id' => $item->user_id,
                'user_show' => $resUser ? $resUser->name : CommonEnum::DEFAULT_NULL,
                'path' => $item->path,
                'method' => $item->method,
                'input' => $user->is_admin == CommonEnum::YES ? $item->input : "无权查看",
                'created_at' => $item->created_at->toDateTimeString()
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }


}

